home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / libpng / pngmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-12  |  8.3 KB  |  312 lines

  1.  
  2. /* pngmem.c - stub functions for memory allocation
  3.  
  4.    libpng 1.0 beta 6 - version 0.96
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    Copyright (c) 1996, 1997 Andreas Dilger
  8.    May 12, 1997
  9.  
  10.    This file provides a location for all memory allocation.  Users which
  11.    need special memory handling are expected to modify the code in this file
  12.    to meet their needs.  See the instructions at each function. */
  13.  
  14. #define PNG_INTERNAL
  15. #include "png.h"
  16.  
  17. /* Borland DOS special memory handler */
  18. #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
  19. /* if you change this, be sure to change the one in png.h also */
  20.  
  21. /* Allocate memory for a png_struct.  The malloc and memset can be replaced
  22.    by a single call to calloc() if this is thought to improve performance. */
  23. png_voidp
  24. png_create_struct(int type)
  25. {
  26.    png_size_t size;
  27.    png_voidp struct_ptr;
  28.  
  29.    if (type == PNG_STRUCT_INFO)
  30.      size = sizeof(png_info);
  31.    else if (type == PNG_STRUCT_PNG)
  32.      size = sizeof(png_struct);
  33.    else
  34.      return (png_voidp)NULL;
  35.  
  36.    if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
  37.    {
  38.       png_memset(struct_ptr, 0, size);
  39.    }
  40.  
  41.    return (struct_ptr);
  42. }
  43.  
  44.  
  45. /* Free memory allocated by a png_create_struct() call */
  46. void
  47. png_destroy_struct(png_voidp struct_ptr)
  48. {
  49.    if (struct_ptr != NULL)
  50.       farfree (struct_ptr);
  51. }
  52.  
  53. /* Allocate memory.  For reasonable files, size should never exceed
  54.  * 64K.  However, zlib may allocate more then 64K if you don't tell
  55.  * it not to.  See zconf.h and png.h for more information. zlib does
  56.  * need to allocate exactly 64K, so whatever you call here must
  57.  * have the ability to do that.
  58.  *
  59.  * Borland seems to have a problem in DOS mode for exactly 64K.
  60.  * It gives you a segment with an offset of 8 (perhaps to store it's
  61.  * memory stuff).  zlib doesn't like this at all, so we have to
  62.  * detect and deal with it.  This code should not be needed in
  63.  * Windows or OS/2 modes, and only in 16 bit mode.  This code has
  64.  * been updated by Alexander Lehmann for version 0.89 to waste less
  65.  * memory.
  66.  *
  67.  * Note that we can't use png_size_t for the "size" declaration,
  68.  * since on some systems a png_size_t is a 16-bit quantity, and as a
  69.  * result, we would be truncating potentially larger memory requests
  70.  * (which should cause a fatal error) and introducing major problems.
  71.  */
  72. png_voidp
  73. png_malloc(png_structp png_ptr, png_uint_32 size)
  74. {
  75.    png_voidp ret;
  76.    if (png_ptr == NULL || size == 0)
  77.       return ((voidp)NULL);
  78.  
  79. #ifdef PNG_MAX_MALLOC_64K
  80.    if (size > (png_uint_32)65536L)
  81.       png_error(png_ptr, "Cannot Allocate > 64K");
  82. #endif
  83.  
  84.    if (size == (png_uint_32)(65536L))
  85.    {
  86.       if (png_ptr->offset_table == NULL)
  87.       {
  88.          /* try to see if we need to do any of this fancy stuff */
  89.          ret = farmalloc(size);
  90.          if (ret == NULL || ((png_size_t)ret & 0xffff))
  91.          {
  92.             int num_blocks;
  93.             png_uint_32 total_size;
  94.             png_bytep table;
  95.             int i;
  96.             png_byte huge * hptr;
  97.  
  98.             if (ret != NULL)
  99.                farfree(ret);
  100.             ret = NULL;
  101.  
  102.             num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
  103.             if (num_blocks < 1)
  104.                num_blocks = 1;
  105.             if (png_ptr->zlib_mem_level >= 7)
  106.                num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
  107.             else
  108.                num_blocks++;
  109.  
  110.             total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
  111.  
  112.             table = farmalloc(total_size);
  113.  
  114.             if (table == NULL)
  115.             {
  116.                png_error(png_ptr, "Out of Memory");
  117.             }
  118.  
  119.             if ((png_size_t)table & 0xfff0)
  120.             {
  121.                png_error(png_ptr, "Farmalloc didn't return normalized pointer");
  122.             }
  123.  
  124.             png_ptr->offset_table = table;
  125.             png_ptr->offset_table_ptr = farmalloc(num_blocks *
  126.                sizeof (png_bytep));
  127.  
  128.             if (png_ptr->offset_table_ptr == NULL)
  129.             {
  130.                png_error(png_ptr, "Out of memory");
  131.             }
  132.  
  133.             hptr = (png_byte huge *)table;
  134.             if ((png_size_t)hptr & 0xf)
  135.             {
  136.                hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
  137.                hptr += 16L;
  138.             }
  139.             for (i = 0; i < num_blocks; i++)
  140.             {
  141.                png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
  142.                hptr += 65536L;
  143.             }
  144.  
  145.             png_ptr->offset_table_number = num_blocks;
  146.             png_ptr->offset_table_count = 0;
  147.             png_ptr->offset_table_count_free = 0;
  148.          }
  149.       }
  150.  
  151.       if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
  152.          png_error(png_ptr, "Out of Memory");
  153.  
  154.       ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
  155.    }
  156.    else
  157.       ret = farmalloc(size);
  158.  
  159.    if (ret == NULL)
  160.    {
  161.       png_error(png_ptr, "Out of Memory");
  162.    }
  163.  
  164.    return ret;
  165. }
  166.  
  167. /* free a pointer allocated by png_malloc().  In the default
  168.    configuration, png_ptr is not used, but is passed in case it
  169.    is needed.  If ptr is NULL, return without taking any action. */
  170. void
  171. png_free(png_structp png_ptr, png_voidp ptr)
  172. {
  173.    if (png_ptr == NULL || ptr == NULL)
  174.       return;
  175.  
  176.    if (png_ptr->offset_table != NULL)
  177.    {
  178.       int i;
  179.  
  180.       for (i = 0; i < png_ptr->offset_table_count; i++)
  181.       {
  182.          if (ptr == png_ptr->offset_table_ptr[i])
  183.          {
  184.             ptr = NULL;
  185.             png_ptr->offset_table_count_free++;
  186.             break;
  187.          }
  188.       }
  189.       if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
  190.       {
  191.          farfree(png_ptr->offset_table);
  192.          farfree(png_ptr->offset_table_ptr);
  193.          png_ptr->offset_table = NULL;
  194.          png_ptr->offset_table_ptr = NULL;
  195.       }
  196.    }
  197.  
  198.    if (ptr != NULL)
  199.       farfree(ptr);
  200. }
  201.  
  202. #else /* Not the Borland DOS special memory handler */
  203.  
  204. /* Allocate memory for a png_struct or a png_info.  The malloc and
  205.    memset can be replaced by a single call to calloc() if this is thought
  206.    to improve performance noticably.*/
  207. png_voidp
  208. png_create_struct(int type)
  209. {
  210.    png_size_t size;
  211.    png_voidp struct_ptr;
  212.  
  213.    if (type == PNG_STRUCT_INFO)
  214.       size = sizeof(png_info);
  215.    else if (type == PNG_STRUCT_PNG)
  216.       size = sizeof(png_struct);
  217.    else
  218.       return (png_voidp)NULL;
  219.  
  220. #if defined(__TURBOC__) && !defined(__FLAT__)
  221.    if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
  222. #else
  223. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  224.    if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
  225. # else
  226.    if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
  227. # endif
  228. #endif
  229.    {
  230.       png_memset(struct_ptr, 0, size);
  231.    }
  232.  
  233.    return (struct_ptr);
  234. }
  235.  
  236.  
  237. /* Free memory allocated by a png_create_struct() call */
  238. void
  239. png_destroy_struct(png_voidp struct_ptr)
  240. {
  241.    if (struct_ptr != NULL)
  242. #if defined(__TURBOC__) && !defined(__FLAT__)
  243.       farfree(struct_ptr);
  244. #else
  245. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  246.       hfree(struct_ptr);
  247. # else
  248.       free(struct_ptr);
  249. # endif
  250. #endif
  251. }
  252.  
  253.  
  254. /* Allocate memory.  For reasonable files, size should never exceed
  255.    64K.  However, zlib may allocate more then 64K if you don't tell
  256.    it not to.  See zconf.h and png.h for more information.  zlib does
  257.    need to allocate exactly 64K, so whatever you call here must
  258.    have the ability to do that. */
  259.  
  260. png_voidp
  261. png_malloc(png_structp png_ptr, png_uint_32 size)
  262. {
  263.    png_voidp ret;
  264.    if (png_ptr == NULL || size == 0)
  265.       return (NULL);
  266.  
  267. #ifdef PNG_MAX_MALLOC_64K
  268.    if (size > (png_uint_32)65536L)
  269.       png_error(png_ptr, "Cannot Allocate > 64K");
  270. #endif
  271.  
  272. #if defined(__TURBOC__) && !defined(__FLAT__)
  273.    ret = farmalloc((png_size_t)size);
  274. #else
  275. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  276.    ret = halloc(size, 1);
  277. # else
  278.    ret = malloc(size);
  279. # endif
  280. #endif
  281.  
  282.    if (ret == NULL)
  283.    {
  284.       png_error(png_ptr, "Out of Memory");
  285.    }
  286.  
  287.    return ret;
  288. }
  289.  
  290. /* Free a pointer allocated by png_malloc().  In the default
  291.   configuration, png_ptr is not used, but is passed in case it
  292.   is needed.  If ptr is NULL, return without taking any action. */
  293. void
  294. png_free(png_structp png_ptr, png_voidp ptr)
  295. {
  296.    if (png_ptr == NULL || ptr == NULL)
  297.       return;
  298.  
  299. #if defined(__TURBOC__) && !defined(__FLAT__)
  300.    farfree(ptr);
  301. #else
  302. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  303.    hfree(ptr);
  304. # else
  305.    free(ptr);
  306. # endif
  307. #endif
  308. }
  309.  
  310. #endif /* Not Borland DOS special memory handler */
  311.  
  312.